#!/bin/sh

set -e

export E2E_PATH="${E2E_PATH:-$(realpath "${0%/*}")}"
export PROJECT_PATH="${PROJECT_PATH:-$E2E_PATH/../..}"
export E2E_PARALLELISM="${E2E_PARALLELISM:-$(getconf _NPROCESSORS_ONLN)}"
export TARGET_PATH="${TARGET_PATH:-$E2E_PATH/target}"
export LOG_PATH="$TARGET_PATH"

if ! command -v local > /dev/null 2>&1
then
  >&2 echo "You are probably using an AT&T Korn Shell or any other shell that does not support local variables in POSIX functions."
  exit 1
fi

E2E_SOURCES="0"

e2e_add_source() {
  eval "E2E_SOURCES_$E2E_SOURCES=$1"
  E2E_SOURCES="$((E2E_SOURCES + 1))"
}

. "$E2E_PATH/helpers"

[ "x" = "x$SHELL_XTRACE" ] || [ "$E2E_TRACE_ALL" = true ] || set +x

UTIL_PATHS="$(find "$E2E_PATH/utils" -type f)"
for UTIL_PATH in $UTIL_PATHS
do
  . "$UTIL_PATH"
done

e2e_list_utils() {
  for UTIL_PATH in $UTIL_PATHS
  do
    echo "$(basename "$UTIL_PATH")"
  done
}

while [ "$E2E_SOURCES" -gt 0 ]
do
  E2E_SOURCES="$((E2E_SOURCES - 1))"
  eval ". \"$E2E_PATH/\$E2E_SOURCES_$E2E_SOURCES\""
done

[ "x" = "x$SHELL_XTRACE" ] || [ "$E2E_TRACE_ALL" = true ] || set -x

if { [ "$(basename "$0")" = "e2e" ] || [ "$E2E_DO_EXEC" = true ]; } && [ "$#" -gt 0 ]
then
  "$@"
fi
